<template>
    <div>
        <Form :label-width="100" inline>
            <Form-item label="年度">
                <DatePicker type="year" v-model="queryParam.cfag0004" placeholder="请选择年度"  :editable="false"></DatePicker>
            </Form-item>
            <Row class="button_row">
                <span style="float: left;">
                <Button type="primary" @click="handleAdd()">录入</Button>
                </span>
                <span style="float: right;">
                      <Button type="primary" @click="searchQueryNew">查询</Button>
                <Button type="primary" class="button_item_left" @click="searchReset">重置</Button>
                </span>
            </Row>
        </Form>
        <Modal v-model="visible" :title="title" width="600" style="position:relative;">
            <Form ref="formData" :model="formData" :label-width="150" :rules="rules">
                <FormItem label="标准类别" prop="cfag0001">
                    <c-dict-select v-model="formData.cfag0001" dictCode="festival" :width="300" :disabled="!showSubmit"
                                   showKeys="1"
                                   placeholder="请选择标准类别"/>
                </FormItem>
                <FormItem label="低保标准(元)" prop="cfag0002">
                    <Input v-model="formData.cfag0002" placeholder="" number style="width: 300px"
                           :disabled="!showSubmit"/>
                </FormItem>
                <FormItem label="低保边缘标准(元)">
                    <Input v-model="formData.cfag0003" placeholder="" number style="width: 300px"
                           :disabled="!showSubmit"/>
                </FormItem>
                <FormItem label="年度" prop="cfag0004">
                    <DatePicker type="year" v-model="formData.cfag0004" placeholder="请选择年度" style="width: 300px"
                                :disabled="!showSubmit"  :editable="false"></DatePicker>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button @click="visible = false">关闭</Button>
                <Button type="primary" @click="handleSubmit('formData')" v-if="showSubmit">确定</Button>
            </div>
        </Modal>
        <div class="table_div">
            <Table :columns="columns" :data="tableData" :loading="Loading">
                <template slot-scope="{ row, index  }" slot="action">
                    <Button type="info" size="small" style="margin-right: 5px" @click="handleEdit(index)">编辑</Button>
                    <Button type="success" size="small" style="margin-right: 5px" @click="handleView(index)">详情</Button>
                    <Button type="error" size="small" style="margin-right: 5px" @click="handleDelete(row.id)">删除
                    </Button>
                </template>
            </Table>
            <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                  @on-change="handleTableChange" :current="pageNo"></Page>
        </div>
    </div>
</template>

<script>
    import {postAction} from '@/api/request'
    import {CommonMixin} from '@/views/common/js/CommonMixin'
    import DateFormat from '@/plugins/format.js'

    export default {
        name: "ss06007",
        mixins: [CommonMixin],
        components: {},
        data() {
            return {
                rules: {
                    cfag0001: [
                        {required: true, message: '标准类型必填', trigger: 'blur'}
                    ],
                    cfag0002: [
                        {required: true, message: '必须是数字类型', trigger: 'blur', type: 'number'}
                    ],
                    cfag0003: [
                        {required: true, message: '必须是数字类型', trigger: 'blur', type: 'number'}
                    ],
                    cfag0004: [
                        {required: true, message: '信息必填', trigger: 'blur', type: 'date'},
                    ]
                },
                columns: [

                    {
                        title: '标准类别',
                        key: 'cfag0001_name',
                        align: "center"
                    },
                    {
                        title: '低保标准(元)',
                        key: 'cfag0002',
                        align: "center"
                    },
                    {
                        title: '低保边缘标准(元)',
                        key: 'cfag0003',
                        align: "center"
                    },
                    {
                        title: '年度',
                        key: 'cfag0004',
                        align: "center"
                    },
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        slot: 'action',
                        width: 300
                    }
                ],
                tableData: [],
                Loading: false,
                url: {
                    list: "/api/ac/subsistence/ss06007Service/getPageList",
                    edit: "/api/ac/subsistence/ss06007Service/updateData",
                    add: "/api/ac/subsistence/ss06007Service/insertData",
                    delete: "/api/ac/subsistence/ss06007Service/deleteData"
                },
                formData: {
                    cfag0001: '',
                    cfag0002: '',
                    cfag0003: '',
                    cfag0004: '',
                },
                queryParam: {
                    cfag0001: '1',
                },
                visible: false,
                showSubmit: true,
                dataCount: 0,
                pageSize: 10,
                modal: false,
                title: ""
            }
        },
        methods: {
            searchQueryNew() {
                this.queryParam.cfag0004 = this.DateFormat(this.queryParam.cfag0004, 'YYYY');
                this.loadData();
            },
            handleAdd() {
                let that = this;
                that.title = "专项救助标准录入"
                let record = {};
                that.visible = true
                that.showSubmit = true
                that.formData = Object.assign({}, record)
            },
            handleEdit(index) {
                let that = this;
                that.title = "专项救助标准编辑"
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.showSubmit = true
                    that.formData = Object.assign({}, record)
                    that.formData.cfag0004 = new Date(record.cfag0004);
                } else {
                    that.visible = false
                }
            },
            handleView(index) {
                let that = this;
                that.title = "专项救助标准查看"
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.showSubmit = false
                    that.formData = Object.assign({}, record)
                    that.formData.cfag0004 = new Date(record.cfag0004);
                } else {
                    that.visible = false
                }
            },
            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let that = this;
                        if (that.formData.cfag0001 == '1' && that.formData.cfag0003 == undefined) {
                            this.$Message.error("两节救助：低保边缘标准必填！");
                            return;
                        }
                        let dataParam = {
                            id: that.formData.id,
                            cfag0001: that.formData.cfag0001,
                            cfag0002: that.formData.cfag0002,
                            cfag0003: that.formData.cfag0003,
                            cfag0004: DateFormat(that.formData.cfag0004, 'YYYY'),
                        }
                        let url = null;
                        if (dataParam.id == null) {
                            url = that.url.add
                        } else {
                            url = that.url.edit
                        }

                        postAction(url, dataParam).then((res) => {
                            if (res.data.data.code == 1) {
                                that.visible = false
                                this.$Message.success("提交成功！")
                                this.loadData();
                            } else {
                                this.$Message.error(res.data.data.msg);
                            }
                        }).finally(() => {

                        })
                    } else {
                        this.$Notice.error({title: '必填信息未填写完成!', desc: '必填信息未填写完成!'})
                    }
                });
            }
        },
        created() {
            this.loadData();
        },
    }
</script>
<style scoped>
    .paging {
        float: right;
        margin-top: 10px;
    }

    /deep/ .ivu-upload-list {
        display: none;
    }
</style>
